﻿/**********************************************
 * 功能名称：中间体定义
编写人：  杨莹
建立时间： 杨 2008年11月19日
说明：
根据操作员的责任中心范围，列表显示当前作业中心的中间体。
 可以增加、修改 、删除中间体信息。
 * 另设计了产品账号的弹出窗体。
 * 注意：这个地方的选择产品不是物流意义上的产品编码。而是财务上的成本科目
 *
 * 
 * 
 **************************************************************/


using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Hisun.Models;
using Hisun.Controllers;
using System.Collections.Generic;

using System.Text;

namespace Hisun.View.ProductInProcess
{
    public partial class PIPSetup : AbcPageBase
    {
        HisunDBDataContext db = new HisunDBDataContext();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {


                var CurrentUserCenterId = (from a in db.TAbc_JS_Users where a.操作员ID == Session[ServerVars.sysSessionUserName].ToString() select new { a.责任部门ID }).Single();
                dropCostCenterSelect.Items.Clear();
                var myquery = from a in db.TABC_JS_CostCenter select a;
                FillData2DropDownList(Convert.ToInt32(CurrentUserCenterId.责任部门ID), myquery);
            }
        }


        //查询当前操作员对应的作业中心列表
        private void FillData2DropDownList(int CenterID, IEnumerable<TABC_JS_CostCenter> TotalTrees)
        {
            var Mylist = (from m in TotalTrees where m.上级作业中心ID == CenterID select m).ToList();

            if (Mylist.Count() == 0)
                return;
            foreach (var MylistItem in Mylist)
            {
                ListItem InsertItem = new ListItem(MylistItem.作业中心名称, MylistItem.作业中心ID.ToString());
                FillData2DropDownList(MylistItem.作业中心ID, TotalTrees);
                if (MylistItem.是否直接成本 == true)
                {
                    dropCostCenterSelect.Items.Add(InsertItem);
                }

            }
        }



        protected void Button_Query_Click(object sender, EventArgs e)
        {
            string Centerid = dropCostCenterSelect.SelectedValue;
            BindMyData(Centerid);
        }

        private void BindMyData(string Centerid)
        {

            int MyCenterId = Convert.ToInt32(Centerid);

            var MyPIPQuery = from a in db.TABC_JS_ProductInProcess
                             join b in db.TABC_JS_Product on a.产品ID equals b.产品账户ID
                             where a.作业中心ID == MyCenterId
                             select new
                             {
                                 ID = a.ID,
                                 部门ID = a.部门ID,
                                 中间体ID = a.中间体ID,
                                 中间体名称 = a.中间体名称,
                                 计量单位 = a.计量单位,
                                 是否中间体 = a.是否中间体,
                                 产品ID = a.产品ID,
                                 产品名称 = b.产品名称,
                                 是否可用 = a.可用
                             };

            GridView_PIP.DataSource = MyPIPQuery;
            GridView_PIP.DataBind();

        }






        //增加按钮
        protected void Button_Add_Command(object sender, CommandEventArgs e)
        {



            Label_PIPId.Text = "";
            TextBox_PIPName.Text = "";
            Label_ProductId.Text = "";
            Label_ProductName.Text = "";
            Label_Unit.Text = "";
            CheckBox_IsProducted.Checked = true;
            TextBox_Organ.Text = "";

            EditFlag.Value = "add";
            Panel_GridList.Visible = false;
            Panel_AddEdit.Visible = true;

        }
        //编辑按钮
        protected void Button_Edit_Command(object sender, CommandEventArgs e)
        {
            int myid = Convert.ToInt32(e.CommandArgument);

            var MyQuery = (from a in db.TABC_JS_ProductInProcess
                           join b in db.TABC_JS_Product on a.产品ID equals b.产品账户ID into mytempb
                           where a.ID == myid
                           from tempb in mytempb.DefaultIfEmpty()

                           select new
                               {
                                   部门ID = a.部门ID,
                                   ID = a.ID,
                                   中间体ID = a.中间体ID,
                                   中间体名称 = a.中间体名称,
                                   计量单位 = a.计量单位,
                                   是否中间体 = a.是否中间体,
                                   产品ID = a.产品ID,
                                   产品名称 = (tempb == null ? "" : tempb.产品账户名称) 
                                   ,是否可用 = a.可用


                               }).Single();



            Label_PIPId.Text = MyQuery.中间体ID;
            TextBox_PIPName.Text = MyQuery.中间体名称;
            Label_ProductId.Text = MyQuery.产品ID;
            Label_ProductName.Text = MyQuery.产品名称;
            Label_Unit.Text = MyQuery.计量单位;
            CheckBox_IsProducted.Checked = Convert.ToBoolean(MyQuery.是否中间体);
            CheckBox_IsUseing.Checked = Convert.ToBoolean(MyQuery.是否可用);
            TextBox_Organ.Text = MyQuery.部门ID;

            EditFlag.Value = "edit";

            Panel_GridList.Visible = false;
            Panel_AddEdit.Visible = true;



        }
        //删除按钮
        protected void Button_Del_Command(object sender, CommandEventArgs e)
        {

            //删除按钮是把当前作业中心的中间体“可用”字段变成FALSE

            int myid = Convert.ToInt32(e.CommandArgument);
            var myquery = (from a in db.TABC_JS_ProductInProcess
                           where a.ID == myid
                           select a).Single();
            myquery.可用 = false;
            try
            {

                db.SubmitChanges();
                Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "message", " <script>alert('删除成功'); </script>");

                string Centerid = dropCostCenterSelect.SelectedValue;
                BindMyData(Centerid);
            }
            catch
            {

                Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "message", " <script>alert('删除失败'); </script>");

            }


        }
        //取消按钮
        protected void Button_Cancel_Command(object sender, CommandEventArgs e)
        {



            Label_PIPId.Text = "";
            TextBox_PIPName.Text = "";
            Label_ProductId.Text = "";
            Label_ProductName.Text = "";
            Label_Unit.Text = "";
            CheckBox_IsProducted.Checked = false;
            TextBox_Organ.Text = "";

            EditFlag.Value = "";
            Panel_GridList.Visible = true;
            Panel_AddEdit.Visible = false;

        }





        //保存按钮
        protected void Button_Save_Command(object sender, CommandEventArgs e)
        {

           


            if (EditFlag.Value == "edit")
            {
                var myrow = (from a in db.TABC_JS_ProductInProcess where a.中间体ID == Label_PIPId.Text select a).Single();

                myrow.中间体名称 = TextBox_PIPName.Text;
                myrow.计量单位 = Label_Unit.Text;
                myrow.产品ID = Label_ProductId.Text;
                myrow.是否中间体 = CheckBox_IsProducted.Checked;
                myrow.可用 = CheckBox_IsUseing.Checked;
                myrow.部门ID = TextBox_Organ.Text;



                try
                {
                    db.SubmitChanges();
                    Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "message", " <script>alert('修改成功'); </script>");
                    string Centerid = dropCostCenterSelect.SelectedValue;
                    BindMyData(Centerid);

                    EditFlag.Value = "";
                    Panel_GridList.Visible = true;
                    Panel_AddEdit.Visible = false;


                }
                catch
                {
                    Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "message", " <script>alert('修改失败'); </script>");

                }



            }
            else if (EditFlag.Value == "add")
            {
                string[] myarray = MyProductInfo.Value.Split('&');
                //判断当前记录是否已存在

                var isExistRow = (from m in db.TABC_JS_ProductInProcess
                                  where m.作业中心ID == Convert.ToInt32(dropCostCenterSelect.SelectedValue)
                                  && m.产品ID == myarray[0]                           
                                  select m).Count();
                if (isExistRow != 0)
                {

                    Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "message", " <script>alert('当前中间体已存在'); </script>");
                    return;
                
                }

                TABC_JS_ProductInProcess addrow = new TABC_JS_ProductInProcess();

               
                //addrow.中间体ID = dropCostCenterSelect.SelectedValue + Label_ProductId.Text;
                addrow.中间体ID = dropCostCenterSelect.SelectedValue +"-" + myarray[0];
                addrow.公司ID = "hisun";
                addrow.中间体名称 = TextBox_PIPName.Text;
                addrow.产品ID = myarray[0];
                addrow.作业中心ID = Convert.ToInt32(dropCostCenterSelect.SelectedValue);
                addrow.计量单位 = Label_Unit.Text;
                addrow.是否中间体 = CheckBox_IsProducted.Checked;
                addrow.可用 = CheckBox_IsUseing.Checked;
                addrow.部门ID = TextBox_Organ.Text;
                addrow.操作员ID = Session[ServerVars.sysSessionUserName].ToString();
                db.TABC_JS_ProductInProcess.InsertOnSubmit(addrow);

                try
                {
                    db.SubmitChanges();
                    Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "message", " <script>alert('建立成功'); </script>");
                    string Centerid = dropCostCenterSelect.SelectedValue;
                    BindMyData(Centerid);
                    EditFlag.Value = "";
                    Panel_GridList.Visible = true;
                    Panel_AddEdit.Visible = false;
                }
                catch
                {
                    Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "message", " <script>alert('建立失败'); </script>");

                }


            }
        }


        //弹出选择框，选择产品账号。
        protected void Button_SelectProductAccount_Click(object sender, EventArgs e)
        {
            StringBuilder s = new StringBuilder();
            s.Append("<script language=javascript>");
            s.Append("var a=window.showModalDialog('PIPSelect.aspx');");
            s.Append("if(a!=null)");
            s.Append(@"{ var aa =a.split(""&"") ;");
            s.Append("document.all('Label_ProductId').value=aa[0];");
            s.Append("document.all('Label_ProductName').value=aa[1];");
            s.Append("document.all('MyProductInfo').value=a;");
            s.Append("document.all('Label_Unit').value=aa[2];}");
            s.Append("</script>");
            Type cstype = this.GetType();
            ClientScriptManager cs = Page.ClientScript;
            string sname = "lt";
            if (!cs.IsStartupScriptRegistered(cstype, sname))
            { cs.RegisterStartupScript(cstype, sname, s.ToString()); }
        }













    }
}
